
package com.jf.cloud.live.controller.platform;

import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.dto.LiveUserDTO;
import com.jf.cloud.live.model.LiveUser;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 直播成员
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("platformLiveUserController")
@RequestMapping("/p/live_user")
@Tag(name = "平台端直播间用户接口")
public class LiveUserController {

    @Autowired
    private LiveUserService liveUserService;

    @Autowired
    private LiveLogService liveLogService;
    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取直播成员列表" , description = "分页获取直播成员列表")
    public ServerResponseEntity<PageVO<LiveUser>> page(@Valid PageDTO pageDTO, @ParameterObject LiveUserDTO liveUserDTO) {
        PageVO<LiveUser> liveUserPage = liveUserService.page(pageDTO, liveUserDTO);
        for (LiveUser record : liveUserPage.getList()) {
            int[] roles = StrUtil.splitToInt(record.getRoles(), StrUtil.COMMA);
            StringBuilder roleStr = new StringBuilder();
            Map<Integer, String> roleMap = new HashMap<>(16);
            roleMap.put(0, "超级管理员,");
            roleMap.put(1, "管理员,");
            roleMap.put(2, "主播,");
            roleMap.put(3, "运营者,");
            for (int role : roles) {
                roleStr.append(roleMap.get(role));
            }
            roleStr.deleteCharAt(roleStr.length() - 1);
            record.setRoles(roleStr.toString());
        }
        return ServerResponseEntity.success(liveUserPage);
    }

    @GetMapping("/info/{liveUserId}")
    @Operation(summary = "获取直播成员" , description = "根据liveUserId获取直播成员")
    public ServerResponseEntity<LiveUser> getByLiveUserId(@ParameterObject @RequestParam Long liveUserId) {
        LiveUser liveUser = liveUserService.getByLiveUserId(liveUserId);
        return ServerResponseEntity.success(liveUser);
    }

    @PostMapping
    @Operation(summary = "保存直播成员" , description = "保存直播成员")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveUserDTO liveUserDTO) throws WxErrorException {
        LiveUser liveUser = mapperFacade.map(liveUserDTO, LiveUser.class);
        liveUser.setLiveUserId(null);
        liveUser.setShopId(Constant.PLATFORM_SHOP_ID);
        liveUserService.saveInfo(liveUser);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新直播成员" , description = "更新直播成员")
    public ServerResponseEntity<Void> update(@Valid @RequestBody LiveUserDTO liveUserDTO) {
        LiveUser liveUser = mapperFacade.map(liveUserDTO, LiveUser.class);
        liveUserService.update(liveUser);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播成员" , description = "根据直播成员id删除直播成员")
    public ServerResponseEntity<Boolean> delete(@RequestParam Long liveUserId) throws WxErrorException {
        return ServerResponseEntity.success(liveUserService.removeAndWeChatById(liveUserId));
    }

    @GetMapping("/getAddProdNum")
    @Operation(summary = "查询商家今日剩余可用添加直播商品、可删除商品库商品次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.ADD_LIVE_ROLE);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(AuthUserContext.get().getTenantId(), LiveInterfaceType.REMOVE_LIVE_ROLE);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }
}
